package com.easy.app.controller.system;


import com.easy.framework.common.model.R;
import com.easy.framework.common.validate.Add;
import com.easy.framework.common.validate.Update;
import com.easy.framework.database.page.Pagination;
import com.easy.framework.poi.utils.ExcelUtils;
import com.easy.framework.web.base.BaseController;
import com.easy.module.log.annotation.Log;
import com.easy.module.log.enums.Operation;
import com.easy.module.system.entity.SysTemplate;
import com.easy.module.system.query.SysTemplateQuery;
import com.easy.module.system.service.ISysTemplateService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 模板信息Controller
 *
 * @author Yang huijing
 * @date 2024-02-21
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/template")
public class SysTemplateController extends BaseController
{

    private final ISysTemplateService sysTemplateService;

    /**
     * 查询模板信息列表
     */

    @PreAuthorize("@perm.hasPermission('system:template$list')")
    @GetMapping("/page")
    public R<Pagination<SysTemplate>> page(SysTemplateQuery query)
    {
        return R.success(sysTemplateService.page(query));
    }

    /**
     * 导出模板信息列表
     */

    @SneakyThrows
    @PreAuthorize("@perm.hasPermission('system:template$export')")
    @Log(title = "模板信息", type = Operation.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysTemplateQuery query)
    {
        List<SysTemplate> list = sysTemplateService.list(query);
        ExcelUtils.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取模板信息详细信息
     */

    @PreAuthorize("@perm.hasPermission('system:template$query')")
    @GetMapping(value = "/{id}")
    public R<SysTemplate> getInfo(@PathVariable("id") Long id)
    {
        return R.success(sysTemplateService.getById(id));
    }

    /**
     * 新增模板信息
     */

    @PreAuthorize("@perm.hasPermission('system:template$add')")
    @Log(title = "模板信息", type = Operation.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated({Add.class}) SysTemplate dto) {
        if(sysTemplateService.exists(SysTemplate::getCode, dto.getCode())) {
            return R.fail("模板编码 [" + dto.getCode() + "] 已存在");
        }
        return R.result(sysTemplateService.save(dto));
    }

    /**
     * 修改模板信息
     */

    @PreAuthorize("@perm.hasPermission('system:template$edit')")
    @Log(title = "模板信息", type = Operation.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated({Update.class}) SysTemplate dto) {
        if(sysTemplateService.exists(SysTemplate::getCode, dto.getCode(), SysTemplate::getId, dto.getId())) {
            return R.fail("模板编码 [" + dto.getCode() + "] 已存在");
        }
        return R.result(sysTemplateService.updateById(dto));
    }

    /**
     * 删除模板信息
     */

    @PreAuthorize("@perm.hasPermission('system:template$remove')")
    @Log(title = "模板信息", type = Operation.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable("ids") Long[] ids)
    {
        return R.result(sysTemplateService.removeBatchByIds(List.of(ids)));
    }
}